package com.maychat.controller;

import com.maychat.controller.base.BaseController;
import com.maychat.pojo.MayChatSystemOptions;
import com.maychat.pojo.Result;
import com.maychat.pojo.UserInfoToken;
import com.maychat.pojo.extended.Message;
import com.maychat.service.AppUpdateService;
import com.maychat.service.impl.RedisServiceImpl;
import com.maychat.websocket.MessageHandler;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RestController
public class RawController extends BaseController {

    @Autowired
    RedisServiceImpl redisService;
    @Autowired
    private AppUpdateService appUpdateService;
    @Autowired
    private MessageHandler messageHandler;

    @GetMapping("/options")
    public Result options() {
        MayChatSystemOptions options = redisService.getSystemOptions();
        return Result.success(options);
    }

    @PostMapping("/checkVersion")
    public Result checkVersion(@NotBlank String version) throws Exception {
        UserInfoToken tokenObj = getTokenObj();
        Map<String, Object> mp = appUpdateService.getLatest(version, tokenObj.getUserId());
        return Result.success(mp);
    }

    @GetMapping("/checkAuth")
    public Result checkAuth() {
        getTokenObj();
        return Result.success(null);
    }

    //TEST
    @RequestMapping("/test")
    public Result test() {
        Message<Object> msg = new Message<>();
        msg.setMessageContent(String.valueOf(System.currentTimeMillis()));
        messageHandler.sendMsg(msg);
        return Result.success();
    }
}
